<template>
	<div class="system-role-container layout-padding">
		<div class="system-role-padding layout-padding-auto layout-padding-view">
			<div class="system-user-search mb15">
				<!-- <el-input size="default" placeholder="请输入角色名称" style="max-width: 180px" v-model="state.tableData.param.param.role_name.val"> </el-input>
				<el-button size="default" type="primary" class="ml10"  @click="onHandleCurrentChange(1)">
					<el-icon>
						<ele-Search />
					</el-icon>
					查询
				</el-button> -->
				<el-button size="default" type="success" v-auth="'/auth/role/add'" @click="onOpenAddRole('add')">
					<el-icon>
						<ele-FolderAdd />
					</el-icon>
					新增角色
				</el-button>
			</div>
			<el-table :data="state.tableData.data" v-loading="state.tableData.loading" style="width: 100%" :default-sort="{ prop: 'create_time', order: 'ascending' }" @sort-change="sortChange">
				<el-table-column prop="id" label="ID" show-overflow-tooltip width="80px"></el-table-column>
				<el-table-column prop="role_name" label="角色名称" show-overflow-tooltip></el-table-column>
				<el-table-column prop="status" label="角色状态" show-overflow-tooltip>
					<template #default="scope">
						<el-tag type="success" v-if="scope.row.status == 1">启用</el-tag>
						<el-tag type="info" v-else>禁用</el-tag>
					</template>
				</el-table-column>
				<el-table-column prop="remark" label="角色描述" show-overflow-tooltip></el-table-column>
				<el-table-column label="创建时间" sortable="custom">
					<template #default="scope">
						<span>{{ dateFormatYMDHMS(scope.row.create_time * 1000)}}</span>
                    </template>
                </el-table-column>
				<el-table-column label="操作" width="150">
					<template #default="scope">
						<el-button size="small" text type="primary" v-auth="'/auth/role/roles'" @click="onOpenEditAuth(scope.row)"
							>权限</el-button>
						<el-button size="small" text type="primary" v-auth="'/auth/role/edit'" @click="onOpenEditRole('edit', scope.row)"
							>修改</el-button>
						<el-button size="small" text type="primary" v-auth="'/auth/role/del'" @click="onRowDel(scope.row)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
			<el-pagination
				@size-change="onHandleSizeChange"
				@current-change="onHandleCurrentChange"
				class="mt15"
				:pager-count="5"
				:page-sizes="[10, 20, 50, 100]"
				v-model:current-page="state.tableData.param.page"
				background
				v-model:page-size="state.tableData.param.limit"
				layout="total, sizes, prev, pager, next, jumper"
				:total="state.tableData.total"
			>
			</el-pagination>
		</div>
		<RoleDialog ref="roleDialogRef" @refresh="getTableData()" />
		<AuthDialog ref="authDialogRef"/>
	</div>
</template>

<script setup name="role">
import { defineAsyncComponent, reactive, onMounted, ref } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import { roleApi } from '/@/api/role/index';
import commonFunction from '/@/utils/commonFunction';
// 定义变量内容
const { dateFormatYMDHMS } = commonFunction();
// 引入组件
const RoleDialog = defineAsyncComponent(() => import('./dialog.vue'));
// 引入组件
const AuthDialog = defineAsyncComponent(() => import('./authDialog.vue'));
// 定义变量内容
const roleDialogRef = ref();
const authDialogRef = ref();
const state = reactive({
	tableData: {
		data: [],
		total: 0,
		loading: false,
		param: {
			param:{
				role_name:{}
			},
            sort:'create_time',
			order:'asc',
			page: 1,
			limit: 10,
		},
	},
});
// 初始化表格数据
const getTableData = () => {
	state.tableData.loading = true;
    roleApi().index(state.tableData.param).then(res=>{
        state.tableData.data = res.data.list;
        state.tableData.total = res.data.total;
        state.tableData.loading = false;
    })
};
// 打开新增角色弹窗
const onOpenAddRole = (type) => {
	roleDialogRef.value.openDialog(type);
};
// 打开修改角色弹窗
const onOpenEditRole = (type, row) => {
	roleDialogRef.value.openDialog(type, row);
};

// 打开修改权限弹窗
const onOpenEditAuth = (row) => {
	authDialogRef.value.openDialog(row);
};

// 删除角色
const onRowDel = (row) => {
	ElMessageBox.confirm(`此操作将永久删除角色名称：“${row.role_name}”，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
	})
		.then(() => {
            roleApi().del({ids:row.id}).then(res=>{
                getTableData();
                ElMessage.success('删除成功');
            })
		}).catch(() => {});
};
//排序变化
const sortChange = ({prop, order}) =>{
    state.tableData.param.sort = prop
    state.tableData.param.order = order == 'ascending'?'asc':order == 'descending'?'desc':''
    getTableData();
};
// 分页改变
const onHandleSizeChange = (val) => {
	state.tableData.param.limit = val;
	getTableData();
};
// 分页改变
const onHandleCurrentChange = (val) => {
	state.tableData.param.page = val;
	getTableData();
};
// 页面加载时
onMounted(() => {
	getTableData();
});
</script>

<style scoped lang="scss">
.system-role-container {
	.system-role-padding {
		padding: 15px;
		.el-table {
			flex: 1;
		}
	}
}
</style>
